<?php


namespace app\controller;

use app\model\Article;
use app\model\Comment as CommentModel;
use app\model\Dianzan;
use app\model\Friends;
use app\model\Images;
use app\model\Questions;
use app\Request;
use think\facade\Db;
use think\facade\Session;
use app\model\User as UserModel;
use app\model\Answer as AnswerModel;

class Center extends Base
{
    public function commentList(){
        //文章的一级回复呢
        //评论的回复，有回复一级评论和回复二级评论，直接在评论表里搜索
        //点赞，文章点赞和评论点赞，在点赞表里获取
        $username=Session::get('username');
        $reply=CommentModel::
            whereRaw("((parentCommentUser=:name AND replyCommentId=0) or replyCommentUser=:num or(art_author=:username and 
            parentCommentId=0 AND replyCommentId=0))&&is_new=1",
            ['name'=>$username,'num'=>$username,'username'=>$username])
            ->order('createTime','desc')
            ->select();
        if($reply){
            return $this->createApi($reply, 'xx回复了你的评论', 200);
        }else{
            return $this->createApi([], '暂无回复', 204);
        }
    }
    public function goodList(){
        $username=Session::get('username');
        $dianzan=Dianzan::whereRaw('(comUser=:name || art_author=:username)&&is_new=1', ['name'=>$username,'username' => $username])
            ->order('create_time','desc')->select();
        if($dianzan){
            return $this->createApi($dianzan, 'xxx点赞了你的评论或文章', 200);
        }else{
            return $this->createApi([], '暂无点赞', 204);
        }
    }
    public function goodRead(Request $request){
        $data=$request->param();
        $good=Dianzan::where('zId',$data['id'])->find();
        $good->is_new=0;
        $good->save();
        if($good->artId==0){
            return $this->createApi(['回复人'=>$good->zanUser,'评论'=>$good->comContent], 'success', 200);
        }
        else{
            return $this->createApi(['回复人'=>$good->zanUser,'评论'=>$good->artTitle], 'success', 200);
        }
    }
    public function commentRead(Request $request){
        $data=$request->param();
        $comment=CommentModel::where('cId',$data['id'])->find();
        $comment->is_new=0;
        $comment->save();
        if($comment->parentCommentId==0&&$comment->replyCommentId==0){//评论文章
            return $this->createApi(['回复人'=>$comment->uName,'文章'=>$comment->artTitle], 'success', 200);
        }else if($comment->parentCommentId!=0&&$comment->replyCommentId==0){//评论一级评论
            $parent_comment=CommentModel::where('cId',$comment->parentCommentId)->find();
            return $this->createApi(['被回复'=>$parent_comment->content,'被回复人'=>$comment->parentCommentUser,'回复'=>$comment->content,'回复人'=>$comment->uName], 'success', 200);
        }else{//评论二级评论
            $reply_comment=CommentModel::where('cId',$comment->replyCommentId)->find();
            return $this->createApi(['被回复'=>$reply_comment->content,'被回复人'=>$comment->replyCommentUser,'回复'=>$comment->content,'回复人'=>$comment->uName], 'success', 200);
        }
    }
    public function questionList(){
        $username=Session::get('username');
        $questions=Questions::select()->toArray();
        $result=[];
        for ($i=0;$i<sizeof($questions);$i++){
            if($questions[$i]['is_friend']==0&&$questions[$i]['is_school']==0){
                $result[]=$questions[$i];
            }
            if($questions[$i]['is_friend']==1){
                $friend=Friends::where('user_name',$questions[$i]['q_user'])
                    ->where('friend_name',$username)
                    ->find();
                if($friend){
                    $result[]=$questions[$i];
                }
            }
            if($questions[$i]['is_school']==1){
                $user1=UserModel::where('userName',$questions[$i]['q_user'])->find();
                $user2=UserModel::where('userName',$username)->find();
                if($user1->userSchool==$user2->userSchool&&$user2->is_confirm==1){
                    $result[]=$questions[$i];
                }
            }
        }
        return $this->createApi($result,'success',200);
    }
    public function questionRead(Request $request){
        $data=$request->param();
        $question=Questions::where('q_id',$data['q_id'])->find();
        $question->is_new=0;
        $question->save();
        $question=$question->toArray();
        $images=Images::where('is_ques',1)->where('q_id',$data['q_id'])->field('img_add')->select();
        $question['images']=$images;
        return $this->createApi($question,'success',200);
    }
    public function answerList(){//作用应该是将关于该用户的回答分组
        $username=Session::get('username');
        $answers=AnswerModel::where('q_user',$username)
            ->order('a_createtime','asc')
            ->select();
        $result=[];
//        for($i=0;$i<sizeof($answers);$i++){
//            $q_content=Questions::where('q_id',$answers['q_id'])->find()->q_content;
//            $result[$q_content]=$answers[$i];
//        }
        for($i=0;$i<sizeof($answers);$i++){
            $q_content=Questions::where('q_id',$answers[$i]['q_id'])->find()->q_content;
            if(!in_array($q_content,array_keys($result))){
                $result[$q_content]=[];
            }
            array_push($result[$q_content],$answers[$i]);
        }
        return $this->createApi($result,'success',200);
    }
    public function answerRead(Request $request){
        $data=$request->param();
        $username=Session::get('username');
        $question=Questions::where('q_content',$data['q_content'])->find()->toArray();
        $images=Images::where('is_ques',1)->where('q_id',$question['q_id'])
            ->field('img_add')->select();
        $question['images']=$images;
        $answers=AnswerModel::where('q_id',$question['q_id'])
            ->order('a_createtime','asc')
            ->select();
//            ->toArray();
        for($i=0;$i<sizeof($answers);$i++){
//            $answer=AnswerModel::where('a_id',$answers[$i]['a_id'])->find();
            $answers[$i]->user_head=$answers[$i]->
//            $answer->is_new=0;
//            $answer->save();
            $images=Images::where('is_answ',1)->where('a_id',$answers[$i]['a_id'])
                ->field('img_add')->select();
            $answers[$i]['images']=$images;
        }
        $question['answers']=$answers;
        return $this->createApi($question,'success',200);

        $answer=AnswerModel::where('a_id',$data['a_id'])->find();
        $answer->is_new=0;
        $answer->save();
        $answer=$answer->toArray();
        $images=Images::where('is_answ',1)->where('a_id',$data['a_id'])->field('img_add')->select();
        $answer['images']=$images;
        return $this->createApi($answer,'success',200);
    }
    public function reward(Request $request){
        $data=$request->param();
        $user=UserModel::where('userName',$data['a_user'])->find();
        $reward=Questions::where('q_id',$data['q_id'])->find()->reward;
        if($data['satisfied']==1){
            $user->userMoney+=$reward;
            $user->save();
            return $this->createApi([],'award successfully',200);
        }
    }

}